package com.xiaoyao.novel.parser;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.crawl.BaseCrawl;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.FileUtil;
import com.xiaoyao.novel.util.UnicodeDecoder;

public class ChuangshiParser {
	private static Logger logger = Logger.getLogger(ChuangshiParser.class);

	/**
	 * 作者,[书名],简介,[类型],图片
	 * 
	 * @param page
	 * @param book
	 * @return
	 */
	public static Book parseDescrib(String page, Book book) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		logger.info(  page );
		String infoRegex = "<div class='cover'>.+?<img.+?src='(.+?)'.+?<div class='info'>(.+?)</div>" +
				".+?<p id='textauthor'>.+?<a.+?>(.+?)</a>";
		Pattern infoPattern = Pattern.compile(infoRegex);
		Matcher matcher = infoPattern.matcher(page);
		if (matcher.find()) {
			if (ClassUtil.isEmpty(book.picPath)) {
				String picUrl = matcher.group(1);
				String fileName = book.bookId + ".jpg";
				fileName = BaseCrawl.downLoadImage(picUrl, fileName); // 抓图片
				book.picPath = FileUtil.PIC_DIR + fileName;
			}
			if (ClassUtil.isEmpty(book.describ)) {
				String describ = matcher.group(2);
				describ = URLEncoder.encode(describ);
				describ = describ.replaceAll("%E2%8B%AF", "");
				describ = URLDecoder.decode(describ);
				book.setDescrib(describ);
			}
			String authorName = matcher.group(3);
			book.setAuthorName(authorName);
		} else {
			logger.error("信息错误!");
		}
		return book;
	}

	public static List<Chapter> parseBook(String page, Book book) {
		logger.debug("开始解析...");
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		page = page.replaceFirst(".+?<div class='list'>", "");
		if (page.contains("作品相关")) {
			page = page.replaceFirst(".+?<div class='list'>", "");
		}
		logger.debug(page);
		List<Chapter> list = new ArrayList<Chapter>();
		book.list = list;
		// <li><a href="http://chuangshi.qq.com/bk/xh/357735-r-25.html" class="green" title="字数：3067&nbsp;&nbsp;&nbsp;&nbsp;更新时间：2014-06-15 21:40:32"><b class="title">第二十一章 读书的方法</b></a></li>
		String bookRegex = "<li>\\s*<a href='(.+?(\\d+)-r-(\\d+)\\.html)' class='green' title=.+?><b class='title'>(.+?)</b></a></li>";
		Pattern bookPattern = Pattern.compile(bookRegex);
		Matcher matcher = bookPattern.matcher(page);
		while (matcher.find()) {
			String title = matcher.group(4).trim();
			logger.debug(title);
//			if (title.startsWith("引子 ") || title.startsWith("前言") || title.startsWith("第") || title.matches("\\d+.+")) {
				Chapter chapter = new Chapter();
				list.add(chapter);
				chapter.bookId = book.bookId;
				chapter.chapterUrl = matcher.group(1);
				chapter.webId = matcher.group(2)+"/"+ matcher.group(3);
				chapter.chapterTitle = title;
				logger.debug(chapter);
//			}
		}
		return list;
	}

	public static void parseChapter(String page, Chapter chapter) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		if (page.matches(".+?id.+?content.+?>.+")) {
			page = page.replaceFirst(".+?id.+?content.+?>", "");
		}
		page = page.replaceFirst(".+?<p>", "<p>");
		page = page.replaceFirst("<.{1,3}div>.+", "");
		page = UnicodeDecoder.unicodeEsc2Unicode(page);
		if (page.contains("\\")) {
			page = page.replaceAll("\\\\", "");
		}
		logger.debug(page);
		chapter.chapterContent = page;
	}
}
